package com.example.mobilesafe.dao;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Xml;

import com.example.mobilesafe.callback.SmsBackUpCallBack;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class SmsBackUpDao {

    // 进度条的进度值
    private static int index = 0;

    /**
     * 短信备份方法
     * @param context 上下文环境
     * @param path 备份文件夹路径
     * @param smsBackUpCallBack 回调接口
     */
    public static void backUp(Context context, String path, SmsBackUpCallBack smsBackUpCallBack) {

        FileOutputStream fos = null;

        Cursor cursor = null;

        try {
            // 1.获取短信备份写入的文件
            File file = new File(path);
            // 2.获取内容解析器，对数据库进行查询
            cursor = context.getContentResolver().query(Uri.parse("content://sms/"), new String[]{"address", "date", "type", "body"}, null, null, null);
            // 3.文件相应的输出流
            fos = new FileOutputStream(file);
            /*
             * 4.序列化数据库中读取的数据，放置到xml中，xml应维护成如下格式：
             * <smss>
             * 		<sms>
             * 			<address></address>
             * 			<date></date>
             * 			<type></type>
             * 			<body></body>
             * 		</sms>
             *
             * 		<sms>
             * 		</sms>
             *
             * 		<sms>
             * 		</sms>
             * </smss>
             */
            XmlSerializer xmlSerializer = Xml.newSerializer();
            // 5.给xml做相应的设置
            xmlSerializer.setOutput(fos,"utf-8");
            xmlSerializer.startDocument("utf-8",true); // DTD(xml规范)
            xmlSerializer.startTag(null,"smss"); // 命名空间起始
            // 6.备份短信总数的指定给进度条，设置进度条的进度值（进度条/对话框进度条）
            if (smsBackUpCallBack != null){
                smsBackUpCallBack.setMax(cursor.getCount());
            }
            // 7.读取数据库中的每一行的数据，写入到xml中
            while (cursor.moveToNext()){
                xmlSerializer.startTag(null,"sms");

                xmlSerializer.startTag(null,"address");
                xmlSerializer.text(cursor.getString(0));
                xmlSerializer.endTag(null,"address");

                xmlSerializer.startTag(null,"date");
                xmlSerializer.text(cursor.getString(1));
                xmlSerializer.endTag(null,"date");

                xmlSerializer.startTag(null,"type");
                xmlSerializer.text(cursor.getString(2));
                xmlSerializer.endTag(null,"type");

                xmlSerializer.startTag(null,"body");
                xmlSerializer.text(cursor.getString(3));
                xmlSerializer.endTag(null,"body");

                xmlSerializer.endTag(null,"sms");

                // 8.每进行一次循环，就要让进度条变化
                index++;
                // 9.ProgressDialog可以在子线程中更新相应的进度条的转变（进度条/对话框进度条）
                if (smsBackUpCallBack != null){
                    smsBackUpCallBack.setProgress(index);
                }
            }
            xmlSerializer.endTag(null,"smss");
            xmlSerializer.endDocument();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
                try {
                    if (cursor != null && fos != null) {
                        cursor.close();
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}